/*******************************************************************************
 * Copyright (c) 2008 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.classdiagram.tabbedproperties.section;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.ValueSpecification;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Section Composite for Property, Port and Parameter. 
 * <p>It manages the type of the element, its multiplicity and its default value. When the multiplicity is
 * modified, it updates the default value section. The same for the type selection. 
 */
public class PropertyTypeAndValueSection extends PropertyViewSection {

	/** key for data in the menu for Value Specification Kind choice button*/
	protected static final String VALUE_SPEC_DATA = "value_spec_data";

	/** type section inside this section */
	final protected TypeSection typeSection;

	/** value specification section*/
	final protected ValueSpecificationSection valueSpecificationSection;

	/**
	 * Creates a new PropertyTypeAndValueSection.
	 */
	public PropertyTypeAndValueSection() {
		this.typeSection = new SpecificTypeSection();
		valueSpecificationSection = new ValueSpecificationSection() {
			@Override
			protected void addNewSpecification(Element container, ValueSpecification newSpecif) {
				super.addNewSpecification(container, newSpecif);
				if(container instanceof Property) {
					((Property)container).setDefaultValue(newSpecif);
				} else if(container instanceof Port) {
					((Property)container).setDefaultValue(newSpecif);
				}
			}
		};
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);

		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		Group group = getWidgetFactory().createGroup(composite, "Value(s) Definition");
		FormData data = new FormData(650, SWT.DEFAULT);
		group.setLayoutData(data);

		FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
		group.setLayout(fillLayout);

		typeSection.createControls(group, tabbedPropertySheetPage);
		valueSpecificationSection.createControls(group, tabbedPropertySheetPage);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void dispose() {
		typeSection.dispose();
		valueSpecificationSection.dispose();
		super.dispose();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void refresh() {
		super.refresh();
		typeSection.refresh();
		valueSpecificationSection.setValueSpecification(getValueSpecification());
		valueSpecificationSection.refresh();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void selectionChanged(ISelection selection) {
		super.selectionChanged(selection);
		typeSection.selectionChanged(selection);
		valueSpecificationSection.selectionChanged(selection);
		valueSpecificationSection.setValueSpecification(getValueSpecification());
	}

	/**
	 * Retrieve the default value associated to the selected UML element, or 
	 * <code>null</code> if no value specification is given for the element. 
	 * @return the ValueSpecification that represents the default value or 
	 * <code>null</code> if no value specification exists
	 */
	private ValueSpecification getValueSpecification() {
		if(umlElement instanceof Property) { // property (or port, which is a child of property)
			return ((Property)umlElement).getDefaultValue();
		} else if(umlElement instanceof Port) {
			return ((Port)umlElement).getDefaultValue();
		}
		return null;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		typeSection.setInput(part, selection);
		valueSpecificationSection.setInput(part, selection);
		super.setInput(part, selection);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void aboutToBeHidden() {
		typeSection.aboutToBeHidden();
		valueSpecificationSection.aboutToBeHidden();
		super.aboutToBeHidden();
	}

	/**
	 * Type section that updates the section when the type is changed. 
	 */
	protected class SpecificTypeSection extends TypeSection {
		/**
		 * {@inheritDoc}
		 */
		@Override
		protected void browseButtonPressed() {
			super.browseButtonPressed();

			// refresh the property section
			PropertyTypeAndValueSection.this.refresh();
		}
	}

}
